package com.me.jingdongshop.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.me.common.base.BaseFragment;
import com.me.common.base.Const;
import com.me.common.base.Global;
import com.me.common.base.OttoBus;
import com.me.common.util.SharedPreUtil;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.ui.activity.AccountCenterActivity;
import com.me.jingdongshop.ui.activity.LoginAndRegisterActivity;
import com.me.jingdongshop.ui.activity.MainActivity;
import com.me.jingdongshop.ui.activity.MyIndentActivity;
import com.me.jingdongshop.ui.view.CircleImageView;
import com.me.jingdongshop.ui.view.UserCenterItemView;
import com.squareup.otto.Subscribe;

/**
 * Created by YL on 2017/5/4.
 * 【我的】，模块
 */
public class MyFragment extends BaseFragment {
    private LinearLayout llRoot;
    private RelativeLayout rvHeader;
    private ImageView imgBg;
    private FrameLayout flHeadPortrait;
    private CircleImageView imgCircle;
    private TextView tvVip;
    private TextView tvPersonalityId;
    private ImageView imgSex;
    private TextView tvFocus;
    private TextView tvFans;
    private LinearLayout llUsercenterWaits;
    private ImageView imgUsercenterWaitPay;
    private ImageView imgUsercenterWaitRecive;
    private ImageView imgUsercenterWaitSend;
    private ImageView imgWaitComments;
    private ImageView imgWaitSalsereturn;
    private TextView tvUsercenterLine;
    private UserCenterItemView itemUsercenterLike;
    private UserCenterItemView itemUsercenterFocus;
    private UserCenterItemView itemUsercenterBrowse;
    private UserCenterItemView itemUsercenterGuess;
    private UserCenterItemView itemUsercenterAccount;
    private TextView tvUsercenterLogin;
    private ImageView rvDefault;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注册Otto事件
        OttoBus.getDefault().register(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OttoBus.getDefault().unregister(this);
    }

    /**
     * 定义接收Otto事件的方法
     */
    @Subscribe
    public void onEvent(Message msg) {
        //如果是登录了或注销了，需要重新根据登录状态刷新当前界面
        if (msg.what == IHttpService.HTTP_LOGIN || msg.what == IHttpService.HTTP_LOGOUT) {
            checkLogin();
            return;
        }
    }

    private void checkLogin() {
        String uid = SharedPreUtil.getString(mActivity, Const.KEY_UID, "");
        String username = SharedPreUtil.getString(mActivity, Const.KEY_USERNAME, "");
        if (TextUtils.isEmpty(uid)) {
            //显示登录按钮
            tvUsercenterLogin.setText("登录/注册");
            tvPersonalityId.setText("我不是QQ会员");
            rvDefault.setVisibility(View.VISIBLE);
            rvHeader.setVisibility(View.GONE);
//            未登录，所有item隐藏


        } else {//之前有登录，获取用户的基本信息（用户，性别，积分），显示在个人中心界面
            String userName = SharedPreUtil.getString(mActivity, Const.KEY_USERNAME, "");
            tvUsercenterLogin.setText("退出登录");
            tvPersonalityId.setText(username);
            rvDefault.setVisibility(View.GONE);
            rvHeader.setVisibility(View.VISIBLE);
            //item点击事件--登录之后才可点击
            initItemClick();

        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_my;
    }

    @Override
    public void initView() {
        //登录状态
        llRoot = (LinearLayout) findView(R.id.ll_root);
        rvHeader = (RelativeLayout) findView(R.id.rv_header);
        imgBg = (ImageView) findView(R.id.img_bg);
        rvDefault = (ImageView) findView(R.id.rv_default);
        flHeadPortrait = (FrameLayout) findView(R.id.fl_head_portrait);
        imgCircle = (CircleImageView) findView(R.id.img_circle);
        tvVip = (TextView) findView(R.id.tv_vip);
        tvPersonalityId = (TextView) findView(R.id.tv_personality_id);
        imgSex = (ImageView) findView(R.id.img_sex);
        tvFocus = (TextView) findView(R.id.tv_focus);
        tvFans = (TextView) findView(R.id.tv_fans);
        llUsercenterWaits = (LinearLayout) findView(R.id.ll_usercenter_waits);
        imgUsercenterWaitPay = (ImageView) findView(R.id.img_usercenter_wait_pay);
        imgUsercenterWaitRecive = (ImageView) findView(R.id.img_usercenter_wait_recive);
        imgUsercenterWaitSend = (ImageView) findView(R.id.img_usercenter_wait_send);
        imgWaitComments = (ImageView) findView(R.id.img_wait_comments);
        imgWaitSalsereturn = (ImageView) findView(R.id.img_wait_salsereturn);
        tvUsercenterLine = (TextView) findView(R.id.tv_usercenter_line);
        itemUsercenterLike = (UserCenterItemView) findView(R.id.item_usercenter_like);
        itemUsercenterFocus = (UserCenterItemView) findView(R.id.item_usercenter_focus);
        itemUsercenterBrowse = (UserCenterItemView) findView(R.id.item_usercenter_browse);
        itemUsercenterGuess = (UserCenterItemView) findView(R.id.item_usercenter_guess);
        itemUsercenterAccount = (UserCenterItemView) findView(R.id.item_usercenter_account);
        tvUsercenterLogin = (TextView) findView(R.id.tv_usercenter_login);

    }

    @Override
    public void initListener() {


        imgUsercenterWaitPay.setOnClickListener(this);
        imgUsercenterWaitRecive.setOnClickListener(this);
        imgUsercenterWaitSend.setOnClickListener(this);
        imgWaitComments.setOnClickListener(this);
        imgWaitSalsereturn.setOnClickListener(this);
        tvUsercenterLogin.setOnClickListener(this);
    }

    /**
     * 自定义条目的点击事件
     */
    private void initItemClick() {
        //喜欢宝贝
        itemUsercenterLike.setOnItemClickListener(new UserCenterItemView.onUserCenterItemClickListener() {
            @Override
            public void onUserCenterItemClick(View itemView) {
                showToast1("喜欢宝贝--进入到收藏界面");

            }
        });

        //关注商家
        itemUsercenterFocus.setOnItemClickListener(new UserCenterItemView.onUserCenterItemClickListener() {
            @Override
            public void onUserCenterItemClick(View itemView) {
                showToast1("关注商家--进入到发现界面");
                MainActivity activity = (MainActivity) getActivity();
                activity.setTab(2);
            }
        });

        //浏览记录
        itemUsercenterBrowse.setOnItemClickListener(new UserCenterItemView.onUserCenterItemClickListener() {
            @Override
            public void onUserCenterItemClick(View itemView) {
                showToast1("浏览界面正在努力开发");
            }
        });

        //猜你喜欢
        itemUsercenterGuess.setOnItemClickListener(new UserCenterItemView.onUserCenterItemClickListener() {
            @Override
            public void onUserCenterItemClick(View itemView) {
                showToast1("猜你喜欢--进入到发现界面");
                MainActivity activity = (MainActivity) getActivity();
                activity.setTab(2);

            }
        });

        //账户中心
        itemUsercenterAccount.setOnItemClickListener(new UserCenterItemView.onUserCenterItemClickListener() {
            @Override
            public void onUserCenterItemClick(View itemView) {
                if (isLogin()) {
                    showToast1("账户中心--进入账户详情");
                    Intent intent = new Intent(mActivity, AccountCenterActivity.class);
                    startActivity(intent);
                }

            }
        });

    }

    private void startTo(Class clzz) {
        Intent intent = new Intent(mActivity, clzz);
        startActivity(intent);
    }

    private boolean isLogin() {
        String uid = SharedPreUtil.getString(Global.mContext, Const.KEY_UID, "");
        String username = SharedPreUtil.getString(Global.mContext, Const.KEY_USERNAME, "");
        if (TextUtils.isEmpty(uid) && TextUtils.isEmpty(username)) {
            Toast.makeText(mActivity, "请登录", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void initData() {
        checkLogin();

    }

    @Override
    public void onClick(View v, int id) {
        switch (id) {
            case R.id.img_usercenter_wait_pay:
                showToast1("待付款--跳到购物车模块");
                MainActivity activity = (MainActivity) getActivity();
                activity.setTab(3);
                break;
            case R.id.img_usercenter_wait_recive:
                showToast1("待收货--跳到我的订单");
                Intent myIndent1 = new Intent(mActivity, MyIndentActivity.class);
                startActivity(myIndent1);
                break;
            case R.id.img_usercenter_wait_send:
                showToast1("待发货--跳到我的订单");
                Intent myIndent2 = new Intent(mActivity, MyIndentActivity.class);
                startActivity(myIndent2);
                break;
            case R.id.img_wait_comments:
                showToast1("待评价--跳到我的订单");
                Intent myIndent3 = new Intent(mActivity, MyIndentActivity.class);
                startActivity(myIndent3);
                break;
            case R.id.img_wait_salsereturn:
                showToast1("退换货--跳到我的订单");
                Intent myIndent4 = new Intent(mActivity, MyIndentActivity.class);
                startActivity(myIndent4);

                break;

//            登录
            case R.id.tv_usercenter_login:
                showToast("-_-么么哒！登录 ");
                if (!isLogin()) {
                    startTo(LoginAndRegisterActivity.class);
                } else {
                    SharedPreUtil.saveString(Global.mContext, Const.KEY_USERNAME, "");
                    SharedPreUtil.saveString(Global.mContext, Const.KEY_UID, "");
                    Toast.makeText(mActivity, "退出登录", Toast.LENGTH_SHORT).show();
                    Message message = new Message();
                    message.what = IHttpService.HTTP_LOGOUT;
                    OttoBus.getDefault().post(message);
                }
                break;
        }
    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {

    }

    @Override
    public void onHttpError(int reqType, String error) {

    }


    private Toast mToast;

    /**
     * 连续打吐司
     *
     * @param msg 吐司信息
     */
    public void showToast1(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(mActivity, "--", Toast.LENGTH_SHORT);
        }
        mToast.setText(msg);
        mToast.show();
    }
}
